package com.usian.comment.service.impl;

import com.usian.comment.feign.UserFeign;
import com.usian.comment.service.ApCommentRepayService;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.comment.dto.CommentRepayDto;
import com.usian.model.comment.dto.CommentRepayLikeDto;
import com.usian.model.comment.dto.CommentRepaySaveDto;
import com.usian.model.comment.pojo.ApComment;
import com.usian.model.comment.pojo.ApCommentLike;
import com.usian.model.comment.pojo.ApCommentRepay;
import com.usian.model.comment.pojo.ApCommentRepayLike;
import com.usian.model.comment.vo.ApCommentRepayVo;
import com.usian.model.comment.vo.ApCommentVo;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ApCommentRepayServiceImpl implements ApCommentRepayService {
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private MongoTemplate mongoTemplate;

    //加载回复列表
    @Override
    public ResponseResult loadCommentRepay(CommentRepayDto dto) {
        //判断参数
        if (dto.getCommentId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer pageSize=10;//设置查询条数
        //根据评论id将评论的回复全部查询出来
        Query query = Query.query(Criteria.where("commentId").is(dto.getCommentId()));
        query.limit(pageSize);
        List<ApCommentRepay> apCommentRepays = mongoTemplate.find(query,ApCommentRepay.class);
        //判断是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){//未登录展示的数据
            return ResponseResult.okResult(apCommentRepays);
        }else {//登录后展示的数据
            ArrayList<String> commentIdList = new ArrayList<>();
            for (ApCommentRepay apCommentRepay : apCommentRepays) {//遍历获取所有回复的id并存入集合
                commentIdList.add(apCommentRepay.getId());
            }
            //获取所有点过赞的回复
            Query query1 = Query.query(Criteria.where("commentRepayId").in(commentIdList).and("authorId").is(user.getId()));
            List<ApCommentRepayLike> apCommentRepayLikes = mongoTemplate.find(query1, ApCommentRepayLike.class);

            ArrayList<ApCommentRepayVo> apCommentRepayVos = new ArrayList<>();//创建一个存储需要展示回复的集合
            if (apCommentRepayLikes.size()>0){//有点赞的评论
                for (ApCommentRepay apCommentRepay : apCommentRepays) {//遍历所有回复的集合
                    ApCommentRepayVo apCommentRepayVo = new ApCommentRepayVo();//创建一个展示信息的对象
                    BeanUtils.copyProperties(apCommentRepay,apCommentRepayVo);//将回复的数据复制到需要展示信息的里面
                    for (ApCommentRepayLike apCommentRepayLike : apCommentRepayLikes) {//遍历点赞信息的集合
                        if (apCommentRepay.getId().equals(apCommentRepayLike.getCommentRepayId())){//判断所有的回复中有没有点赞的
                            apCommentRepayVo.setOperation(0);//有的话就设置状态，0为点赞
                            break;
                        }else {
                            apCommentRepayVo.setOperation(1);//没有就设置状态1
                            break;
                        }
                    }
                    apCommentRepayVos.add(apCommentRepayVo);//将需要展示的对象存到集合中
                }
                return ResponseResult.okResult(apCommentRepayVos);
            }else {//没有点赞的评论
                return ResponseResult.okResult(apCommentRepays);
            }
        }
    }

    //保存回复
    @Override
    public ResponseResult saveCommentRepay(CommentRepaySaveDto dto) {
        //判断参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //根据登录用户的id查询用户
        ApUser apUser = userFeign.findByUserId(user.getId());
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN,"登录信息有误");
        }
        //封装结果
        ApCommentRepay apCommentRepay = new ApCommentRepay();
        apCommentRepay.setAuthorId(apUser.getId());
        apCommentRepay.setAuthorName(apUser.getName());
        apCommentRepay.setCommentId(dto.getCommentId());
        apCommentRepay.setContent(dto.getContent());
        apCommentRepay.setLikes(0);
        apCommentRepay.setLatitude(null);
        apCommentRepay.setLongitude(null);
        apCommentRepay.setCreatedTime(new Date());
        apCommentRepay.setUpdatedTime(new Date());
        //执行保存
        mongoTemplate.insert(apCommentRepay);
        //评论的回复数+1
        ApComment apComment = mongoTemplate.findById(dto.getCommentId(), ApComment.class);
            apComment.setReply(apComment.getReply()+1);
            mongoTemplate.save(apComment);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //点赞回复
    @Override
    public ResponseResult saveCommentRepayLike(CommentRepayLikeDto dto) {
        //检查参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //根据登录用户的id查询用户
        ApUser apUser = userFeign.findByUserId(user.getId());
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN,"登录信息有误");
        }
        //根据评论回复的id查询
        ApCommentRepay ApCommentRepay = mongoTemplate.findById(dto.getCommentRepayId(), ApCommentRepay.class);
        if (dto.getOperation()==0){//点赞
            ApCommentRepay.setLikes(ApCommentRepay.getLikes()+1);//喜欢数+1
            mongoTemplate.save(ApCommentRepay);
            //保存点赞回复的数据
            ApCommentRepayLike apCommentRepayLike = new ApCommentRepayLike();
            apCommentRepayLike.setAuthorId(apUser.getId());
            apCommentRepayLike.setCommentRepayId(dto.getCommentRepayId());
            apCommentRepayLike.setOperation(dto.getOperation());
            mongoTemplate.insert(apCommentRepayLike);
        }else {//取消点赞
            if (ApCommentRepay.getLikes()>0){//判断喜欢数大于0
                ApCommentRepay.setLikes(ApCommentRepay.getLikes()-1);//喜欢数就-1
                mongoTemplate.save(ApCommentRepay);
            }
            //创建删除条件
            Query query = Query.query(Criteria.where("authorId").is(apUser.getId()).and("commentRepayId").is(dto.getCommentRepayId()));
            //执行删除
            mongoTemplate.remove(query,ApCommentRepayLike.class);
        }
        //返回执行结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
